<?php defined('SYSPATH') or die('No direct script access.');
 
class Snippet
{
	// parsuje treść strony w poszukiwaniu ''[[]]''
	public static function parse($str,$page_id=null)
	{
		$split = explode('[[',$str);
		$output = '';
		//return Kohana::debug($split);exit;
		foreach($split as $s)
		{
			// szukanie znacznika ']]'
			$match = preg_match('|\]\]|',$s);
			// jeżeli nie znalezione - przepisanie całej linijki
			if(!$match)
			{
				$output .= $s;
			}else{
				// jeżeli znalezione...
				// podział na części
				$tiny = explode("]]",$s);
				// pierwsza część do parsera z return
				$output .= Snippet::get_snippet($tiny[0],$page_id);
				// druga część do przepisania
				$output .= $tiny[1];
			}
		}
		return $output;
	/* 	ob_start();
		eval('?>' . stripslashes($output) . ' <?php '); 
		ob_get_contents(); */
	}
	
	// 'router' snippetów - wydobywa treść rodzaju snippeta
	public static function get_snippet($inp,$page_id=null)
	{
		$parts = explode("=",$inp);
		if(isset($parts[1]))
			$id = $parts[1];
		$funkcja = $parts[0];
		switch($funkcja){
			case('element'):
				return Snippet::get($id,true);
			break;
			case('lista galerii'):
				return Snippet::galleries_list();
			break;
			case('children'):
				return Snippet::children($page_id);
			break;
		}
	}
	
	// wyświetla we fronendzie element o podanym id
	// wpisywane do textarea w panelu
	public static function get($id,$inner=false)
	{
		if(!Session::instance()->get('id_lang'))
		{
			$id_lang = Kohana::config('appconf.default_lang_id');
			Session::instance()->set('id_lang', $id_lang);
		}else{
			$id_lang = Session::instance()->get('id_lang');
		}
		$snippet = ORM::factory('snippetslanguage')->where('snippet_id','=',$id)->and_where('lang_id','=',$id_lang)->find()->content;
		
		if($inner == true)
		{
			return $snippet;
			/* eval('?>'.$snippet); */
		}else{
			ob_start();
			eval('?>' . stripslashes($snippet) . ' <?php '); 
			ob_get_contents();
		}
	}
	
	// wyświetla listę galerii
	public static function galleries_list()
	{
		$view = View::factory('frontend/gallery_list');
		$gallery_model = new Model_Cms_Gallery;
		$galleries = $gallery_model->get_galleries(true);
		//echo Kohana::debug($galleries);exit;
		$view->galleries = $galleries;
		return $view;
	}
	
	// wyświetla we frontendzie listę tagów (w danym języku)
	public static function tags()
	{
		$lang_id = Session::instance()->get('id_lang');
		$view = View::factory('frontend/snippet_tags');
		$tag_model = new Model_Cms_Tagsfrompages;
		$view->tags = $tag_model->get_tags_front($lang_id);
		echo $view;
	}
	
	private static function children($page_id)
	{
		$page_model = new Model_Cms_Page;
		$count = $page_model->count_page_children($page_id);
		
		$view = View::factory('frontend/category_show_realizations');
		$pagination = Pagination::factory(array(
					'total_items'    => $count,
					'items_per_page' => Kohana::config('appconf.items_per_page')
				));
		$children = $page_model->get_page_children($page_id,$pagination->items_per_page,$pagination->offset);
		$view->pages = $children;
		$view->page_links = $pagination->render();
		return $view;
	}
	
	public static function newsletter()
	{
		$view = View::factory('frontend/newsletter_box');
		echo $view;
	}
	
	public static function last_commented()
	{
		$view = View::factory('frontend/comments_last');
		$comments_model = new Model_Cms_Comments;
		$comments = $comments_model->get_comments(3);
		$view->comments = $comments;
		echo $view;
	}
	
	public static function posts_navigation($url)
	{
		$page_model = new Model_Cms_Page;
		$page = $page_model->get_page($url);
		$count = $page_model->get_pages_sub_count($page['category']);
		if($page['category'] != 0 && $count > 1)
		{
			$previous = $page_model->nav_previous($page['category'],$page['created_at']);
			$next = $page_model->nav_next($page['category'],$page['created_at']);
			$view = View::factory('frontend/previous_next');
			$view->previous = $previous;
			$view->previous_url = Helpers::get_post_url($page['category'])."/".$previous['url'];
			$view->next = $next;
			$view->next_url = Helpers::get_post_url($page['category'])."/".$next['url'];
			$cat_url = "lll";		
			$view->cat_url = $cat_url;
			return $view;
		}
	}
	
	public static function polls()
	{
		
		$polls_model = new Model_Cms_Polls;
		$id = $polls_model->get_active_polls();
		if($id != null)
		{
			$view = View::factory('frontend/polls_box');
			$name = $polls_model->get_name($id);
			$questions = $polls_model->get_questions($id);
			$view->polls_title = $name;
			$view->id_active = $id;
			$view->questions = $questions;
			return $view;
		}else{
			return null;
		}
	}
	
	public static function banner($id)
	{
		$banner_model = new Model_Futbol_Banners;
		$banner = $banner_model->get_banner($id); 
		$folder = $_SERVER['DOCUMENT_ROOT'].Kohana::$base_url."/upload/banery/".$banner['folder']."/";
		$folder = str_replace('//','/',$folder);
		
		$units = $banner_model->get_units($id);
		$length = $banner_model->count_units($id); // ilość banerów w folderze
		
		$set = array();
		$i = 0;
		foreach($units as $u)
		{
			$set[$i]['name'] = $u['name'];
			$set[$i]['ext'] = $u['ext'];
			$set[$i]['url'] = $u['url'];
			$i++;
		}
			
		if($length > 0)
		{
			if($banner['rotation'] == 1) // wyświetlanie rotacyjne
			{
				if(!isset($_COOKIE['baner_'.$id]))
				{
					setcookie('baner_'.$id, 0, time()+3600);
					$ban_id = 0;
				}else{
					$ban_id = $_COOKIE['baner_'.$id] + 1;
					setcookie('baner_'.$id, $ban_id, time()+3600);
				}
				if($ban_id >= $length)
				{
					setcookie('baner_'.$id, 0, time()+3600);
					$ban_id = 0;
				}
				
				if($set[$ban_id]['ext'] == 'swf')
				{
					$view = View::factory('banners/banner_swf');
					$view->banner = $banner;
				}else{
					$view = View::factory('banners/banner_jpg');
					$view->banner = $banner;
				}
				
				if($set[$ban_id]['name'] == '')
				{
					$view->file = "";
					$view->width = 0;
					$view->height = 0;
					$view->url = null;
				}else{
					
					$file = "upload/banery/".$banner['folder']."/".$set[$ban_id]['name'];
					$view->file = $file;
					$dim = getimagesize($file);
					$view->width = $dim[0];
					$view->height = $dim[1];
					$view->url = $set[$ban_id]['url'];
				}
			}else{ // wyświetlanie kolejno
				//$view = Kohana::debug($units);
				$html = "";
				$i = 0;
				foreach($units as $u)
				{
					$i++;
					if($u['ext'] == 'swf')
					{
						$view = View::factory('banners/banner_swf_tabs');
					}else{
						$view = View::factory('banners/banner_jpg_tabs');
					}
					$view->banner = $banner;
					$file = "upload/banery/".$banner['folder']."/".$u['name'];
					$view->file = $file;
					$dim = getimagesize($file);
					$view->width = $dim[0];
					$view->height = $dim[1];
					$view->url = $u['url'];
					$view->id = $i;
					$html .= "<div style=\"float:left;clear:both;margin-top:10px;\">".$view."</div>\n";
				}
				$view = $html;
			}
			return $view;
		}else{
			return null;
		}
	}
	
	public static function get_page_content($id)
	{
		$page_model = new Model_Cms_Page;
		$page = $page_model->get_page_by_id($id);
		return $page['html'];
	}
}
?>